﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class Profile
    {
        public int ProfileID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int Order { get; set; }
        public int ReturnValue { get; set; }

        public Profile()
        {

        }

        ~Profile()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Profile theo ID
        /// </summary>
        /// <param name="profileID"></param>
        /// <returns></returns>
        public Profile Get(int profileID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Profile>("sp_Profile_Select",
                new SqlParameter("@ProfileID", profileID));
        }

        /// <summary>
        /// Lấy giá trị Profile theo ID
        /// </summary>
        /// <returns></returns>
        public Profile Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<Profile>("sp_Profile_Select",
                new SqlParameter("@ProfileID", ProfileID));
        }

        /// <summary>
        /// Thêm mới Profile
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@Name", Name);
            pars[2] = new SqlParameter("@Order", Order);
            pars[3] = new SqlParameter("@Description", Description);

            db.ExecuteNonQuerySP("sp_Profile_Insert", pars);
            ProfileID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Profile
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@ProfileID", ProfileID);
            pars[2] = new SqlParameter("@Name", Name);
            pars[3] = new SqlParameter("@Order", Order);
            pars[4] = new SqlParameter("@Description", Description);

            db.ExecuteNonQuerySP("sp_Profile_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Profile
        /// </summary>
        /// <param name="profileID"></param>
        public void Delete(int profileID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Profile_Delete",
                new SqlParameter("@ProfileID", profileID));
        }

        /// <summary>
        /// Xoá Profile
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_Profile_Delete",
                new SqlParameter("@ProfileID", ProfileID));
        }

        public DataTable GetTList()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_Profile_SelectList");
        }

        public List<Profile> GetList()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<Profile>("sp_Profile_SelectList");
        }
    }
}
